package cn.wanda.bi.sunshine.platform.server.config.redis;


import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.stereotype.Component;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;



@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
@Component("myShardedRedisCache")
public class MyShardedRedisCache implements Cache {
	
    public static final Logger logger = LoggerFactory.getLogger(MyShardedRedisCache.class);
	
    private static final int PAGE_SIZE = 128;
	private String name;
	
	@Autowired
	private ShardedJedisPool shardedJedisPool;
	
	@Autowired
	@Qualifier("keySerializer")
	private RedisSerializer keySerializer;
	
	@Autowired
	@Qualifier("valueSerializer")
	private RedisSerializer valueSerializer;
	
	private  byte[] prefix;
	private  byte[] setName;
	private  long expiration;
	
	
	@Override
	public ValueWrapper get(Object key) {
		logger.info("======get from cache key : "+ key +"======");
		ShardedJedis jedis = null;
		try {
			byte[] bs=null;
			Object value;
			byte[] computeKey = key.toString().getBytes();
			jedis = shardedJedisPool.getResource();
			bs = jedis.get(computeKey);
			value = valueSerializer.deserialize(bs);
			return (bs == null ? null : new SimpleValueWrapper(value));
		} catch (SerializationException e) {
			e.printStackTrace();
			return null;
		} finally {
			if(jedis!=null) {
				jedis.close();
			}
		}
	}

	@Override
	public void put(Object key, Object value) {
		logger.info("======put to cache key : "+ key +"======");
		ShardedJedis jedis = null;
		try {
			byte[] k = key.toString().getBytes();
			byte[] v = valueSerializer.serialize(value);
			jedis = shardedJedisPool.getResource();
			jedis.set(k, v);
			if(key.toString().startsWith("cache:menupermissions:")) {
				jedis.expire(k, 60);
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
//		}else if(key.toString().startsWith("cache:menupermissions:")) {
			}else {
				jedis.expire(k, 3600);
			}
		} catch (SerializationException e) {
			e.printStackTrace();
		} finally {
			if(jedis!=null) {
				jedis.close();
			}
		}
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Object getNativeCache() {
		return shardedJedisPool;
	}

	@Override
	public <T> T get(Object key, Class<T> type) {
		ShardedJedis jedis = null;
		try {
			byte[] computeKey = key.toString().getBytes();
			jedis = shardedJedisPool.getResource();
			byte[] bs = jedis.get(computeKey);
			Object value = valueSerializer.deserialize(bs);
			return (T) value;
		} catch (SerializationException e) {
			e.printStackTrace();
			return null;
		} finally {
			if(jedis!=null) {
				jedis.close();
			}
		}
	}

	@Override
	public void evict(Object key) {
		logger.info("======delete from cache key : "+ key +"======");
		ShardedJedis jedis=null;
		try {
			jedis = shardedJedisPool.getResource();
			jedis.del(key.toString().getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(jedis!=null) {
				jedis.close();
			}
		}
	}

	@Override
	public <T> T get(Object key, Callable<T> valueLoader) {
		return null;
	}

	@Override
	public ValueWrapper putIfAbsent(Object key, Object value) {
		return null;
	}

	@Override
	public void clear() {
		shardedJedisPool.close();
	}
	
}